/**
 * 
 */
package com.silent.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.silent.domain.Girl;
import com.silent.domain.Result;
import com.silent.repository.GirlRepository;
import com.silent.service.GirlService;
import com.silent.utils.ResultUtil;

/**
 * @author Silent-Y
 *
 */
@RestController
public class GirlController {
	
	private final static Logger logger = LoggerFactory.getLogger(GirlController.class);
	
	@Autowired
	private GirlRepository girlRepository;
	
	@Autowired
	private GirlService girlService;
	
	/**
	 * 查询所有女生
	 * @return
	 */
	@GetMapping(value = "/girls")
	public List<Girl> girlList(){
		
//		System.out.println("方法执行中");
		logger.info("方法执行中");
		return girlRepository.findAll();
	}
	
	/**
	 * 添加一个女生
	 * @param girl
	 * @return
	 */
	@PostMapping(value = "/girls")
	public Result<Girl> girlAdd(@Valid Girl girl,BindingResult bindingResult){
		
		/**
		 * valid标记要验证的对象，在对象类中标记要验证的属性
		 * if代码块为验证结果，如果产生错误，输出错误信息，终止执行后面的代码
		 */
		if(bindingResult.hasErrors()){
//			System.out.println(bindingResult.getFieldError().getDefaultMessage());
		
//			result.setData(null);
			return ResultUtil.error(1, bindingResult.getFieldError().getDefaultMessage());
		}

		girl.setCupSize(girl.getCupSize());
		girl.setAge(girl.getAge());
		
		return ResultUtil.success(girlRepository.save(girl));	
	}
	
	/**
	 * 通过id查询一个女生
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/girls/{id}")
	public Girl girlFindOne(@PathVariable("id") Integer id){
		
		return girlRepository.findOne(id);
	}
	
	/**
	 * 通过id更新一个女生
	 * @param id
	 * @param cupSize
	 * @param age
	 * @return
	 */
	@PutMapping(value = "girls/{id}")
	public Girl girlUpdate(@PathVariable("id") Integer id,
			@RequestParam("cupSize") String cupSize,
			@RequestParam("age") Integer age){
		
		Girl girl = new Girl();
		girl.setId(id);
		girl.setAge(age);
		girl.setCupSize(cupSize);
		
		return girlRepository.save(girl);	
	}
	
	/**
	 * 通过id删除一个女生
	 * @param id
	 */
	@DeleteMapping(value = "girls/{id}")
	public void girlDelete(@PathVariable("id") Integer id){
		
		girlRepository.delete(id);
	}
	
	/**
	 * 通过年龄查询一个女生
	 * @param age
	 * @return
	 */
	@GetMapping(value = "/girls/age/{age}")
	public List<Girl> findByAge(@PathVariable("age") Integer age){
		
		return girlRepository.findByAge(age);
	}
	
	@PostMapping(value = "/girls/two")
	public void GirlTwo(){
		
		girlService.insertTwo();
		
	}
	
	@GetMapping(value = "/girls/getAge/{id}")
	public void getAge(@PathVariable("id") Integer id)throws Exception{
		girlService.getAge(id);
	}
	
}
